const db = require("../db/index")
const path = require('path')

exports.addArticle = (req, res) => {
    if (!req.file || req.file.fieldname !== 'cover_img') return res.cc('文章封面是必选参数！')
    const articleInfo = {
        ...req.body,
        cover_img: path.join('/uploads', req.file.fieldname),
        pub_date: new Date(),
        author_id: req.user.id
    }

    const sql = 'insert into ev_articles set ?'
    db.query(sql,articleInfo,(err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows !== 1) return res.cc('添加文章失败！')
        res.cc('添加文章成功', 0)
    })

}